package com.cx.dao;

import com.cx.pojo.Department;
import com.cx.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther: Centaury
 * @Date:2023/3/19 - 03 - 19 - 21:51
 */
@Repository
public class EmployeeDao {

    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentdao;
    static{
        employees = new HashMap<>();
        employees.put(101,new Employee(101,"AA","210278885@qq.com",1,new Department(1001,"市场部")));
        employees.put(102,new Employee(102,"BB","210278885@qq.com",0,new Department(1002,"营销部")));
        employees.put(103,new Employee(103,"CC","210278885@qq.com",1,new Department(1003,"运营部")));
        employees.put(104,new Employee(104,"DD","210278885@qq.com",0,new Department(1004,"后勤部")));
        employees.put(105,new Employee(105,"EE","210278885@qq.com",1,new Department(1005,"人力部")));
    }

    //自增主键
    public static Integer initId = 106;

    public void save(Employee employee){

        if(employee.getId() == null){
            employee.setId(initId++);
        }

        employee.setDepartment(departmentdao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }

    // 获取所有员工
    public Collection<Employee> getAll(){
        return employees.values();
    }

    // 根据id查询员工
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    // 根据id删除员工
    public void delete(Integer id){
        employees.remove(id);
    }
}


